package com.bailugansan.efficiencyboard.controller;

import com.bailugansan.efficiencyboard.annotation.UserLoginToken;
import com.bailugansan.efficiencyboard.entity.pool.AddPoolEntity;
import com.bailugansan.efficiencyboard.entity.pool.RemovePoolEntity;
import com.bailugansan.efficiencyboard.entity.pool.UpdatePoolEntity;
import com.bailugansan.efficiencyboard.entity.task.AddTaskEntity;
import com.bailugansan.efficiencyboard.entity.task.DeleteTaskEntity;
import com.bailugansan.efficiencyboard.entity.task.SwapTaskEntity;
import com.bailugansan.efficiencyboard.entity.task.UpdateTaskEntity;
import com.bailugansan.efficiencyboard.service.PoolService;
import com.bailugansan.efficiencyboard.service.TaskService;
import com.bailugansan.efficiencyboard.utils.R;
import com.bailugansan.efficiencyboard.vo.PoolVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author BailuGansan
 */

@RestController
@RequestMapping("/crud")
public class ApiController {
    @Autowired
    private PoolService poolService;

    @Autowired
    private TaskService taskService;

    @GetMapping(value = "/data")
    @UserLoginToken
    public R getData(HttpServletRequest httpServletRequest){
        String uid = httpServletRequest.getHeader("Uid");
        List<PoolVo> data = poolService.getPoolVos(uid);
        return R.ok().put("data", data);
    }

    @PostMapping(value = "/addPool")
    @UserLoginToken
    public R addPool(@RequestBody AddPoolEntity entity,
                     HttpServletRequest httpServletRequest){

        // 这里只取头部的uid
        String uid = httpServletRequest.getHeader("Uid");

        boolean result = poolService.addPool(entity, uid);

        if(result){
            return R.ok().put("data", "Pool add success");
        } else {
            return R.error("error: Pool add fail");
        }
    }

    @PostMapping(value = "/updatePool")
    @UserLoginToken
    public R updatePool(@RequestBody UpdatePoolEntity entity,
                    HttpServletRequest httpServletRequest){

        String uid = httpServletRequest.getHeader("Uid");

        boolean result = poolService.updatePool(entity, uid);

        if(result){
            return R.ok().put("data", "Pool update success");
        } else {
            return R.error("error: Pool update fail");
        }
    }

    @PostMapping(value = "/removePool")
    @UserLoginToken
    public R removePool(@RequestBody RemovePoolEntity entity,
                        HttpServletRequest httpServletRequest){

        String uid = httpServletRequest.getHeader("Uid");

        boolean result = poolService.removePool(entity, uid);

        if(result){
            return R.ok().put("data", "Pool remove success");
        } else {
            return R.error("error: Pool remove fail");
        }
    }

    @PostMapping(value = "/addTask")
    @UserLoginToken
    public R addTask(@RequestBody AddTaskEntity entity){
        System.out.println("taskEntity:" + entity);
        // long类型数据末尾加"L"
        boolean result = taskService.addTask(entity);

        if(result){
            return R.ok().put("data", "Task add success");
        } else {
            return R.error("error: Task add fail");
        }
    }

    @PostMapping(value = "/updateTask")
    @UserLoginToken
    public R updateTask(@RequestBody UpdateTaskEntity entity){

        boolean result = taskService.updateTask(entity);

        if(result){
            return R.ok().put("data", "Task update success");
        } else {
            return R.error("error: Task update fail");
        }
    }

    @PostMapping(value = "/deleteTask")
    @UserLoginToken
    public R deleteTask(@RequestBody DeleteTaskEntity entity){

        boolean result = taskService.deleteTask(entity);

        if(result){
            return R.ok().put("data", "Task delete success");
        } else {
            return R.error("error: Task delete fail");
        }
    }

    @PostMapping(value = "/swapTask")
    @UserLoginToken
    public R swapTask(@RequestBody SwapTaskEntity entity){
        boolean result = taskService.swapTask(entity);
        if(result){
            return R.ok().put("data", "Task swap success");
        } else {
            return R.error("error: Task swap fail");
        }
    }
}
